/**
 * Created by LiuJie on 2017/10/11.
 */
/**
 * Created by liujie on 2017/10/6.
 * 监控管理js
 */
var watchCars = [];
var watchCarIds = [];
var isClickingCarData = null;
var speed = 600;
var zdgzCarIds = [];
var isSubmitData = false;
;(function ($,window,undefined) {
    function MonitorView() {
        this.queryTreeData_url = "/monitor/queryTreeData";
        this.queryTreeNodeInfo = "/monitor/queryTreeNodeInfo";
        this.queryCarByStatus_url = "/monitor/queryCarStatus";
        this.queryDevice_url = "/monitor/queryDeviceInfo";
        this.queryCarCount_url = "/monitor/listByCountCar";
        this.queryCarCountMain_url = "/monitor/listByMainCarCount";
        this.$leftTreeArrow = $("#left-tree-arrow");
        this.$leftTree = $("#left-tree");
        this.$bottomArrow = $("#bottom-arrow");
        this.$treeview =$("#treeview");
        this.$searchIcon =$("#search-icon");
        this.$headTab = $("#head-Tab");
        this.$bottomBoxArrow = $("#bottom-box-arrow");
        this.$bottomBox = $("#bottom-box");
        this.$bottomTopArrow = $("#bottom-top-arrow");
        this.myMap = null;
        this.carInfoData = [];
        this.carInfoTable=null;
        this.carInfoTree = null;
        this.oldCarInfoIds = [];

    }
    MonitorView.prototype = {
        initBindEvent:function () {
            var _this = this;
            this.$leftTreeArrow.off("click").on("click",function () {
                $(this).fadeOut(300);
                $("#left-box-id").css({"display":"block"}).animate({
                    "width":"240px"
                },speed);
                // _this.$leftTree.fadeIn(300,function () {
                //     _this.$treeview.show();
                // });
            });
            this.$bottomArrow.off("click").on("click",function () {
                // _this.$treeview.hide();
                $("#left-box-id").animate({
                    "width":"0px"
                },speed,function () {
                    $("#left-box-id").css({"display":"none"});
                    _this.$leftTreeArrow.show()
                });
                // _this.$leftTree.fadeOut(300);
                // _this.$leftTreeArrow.fadeIn(300)
            });
            this.$searchIcon.off("click").on("click",function () {

            });
            this.$headTab.off("click").on("click","li",function () {
                var _tabCode = $(this).attr("tabCode");
                _this._searchByTabCode(_tabCode);
            });
            this.$bottomBoxArrow.off("click").on("click",function () {
                $(this).hide();
                $("#bottom-box").css({"display":"block"});
                $("#bottom-box").animate({
                    "height":"200px"
                },speed);
            });
            this.$bottomTopArrow.off("click").on("click",function () {
                $("#bottom-box").animate({
                    "height":"0px"
                },speed,function () {
                    $("#bottom-box").css({"display":"none"});
                    _this.$bottomBoxArrow.show();
                });
                // _this.$bottomBox.slideUp(600,function () {
                //     _this.$bottomBoxArrow.fadeIn(300);
                // });
            });
            $("#box-first").on("click",function () {
                $("#guanzhuDialog").show();
                //reloadGuanzhuInfoTable();
                //_this.getListMainCarCount();
            });
            $(".box-second-unline").on("click",function () {
                $("#unonlineDialog").show();
            })
            $(".box-second-onrunline").on("click",function () {
                $("#norunlineDialog").show();
            })
            $("#box-second-unTimeline").on("click",function () {
                $("#unonTimelineDialog").show();
            });
            $("#moreDataFenxi").on("click",function () {
                $("#moreDataDialog").show();
            });
            $("#moreDataClose").on("click",function () {
                $("#moreDataDialog").hide();
            });
        },
        initCarInfoTable:function () {
            var _this = this;
            this.carInfoTable = new BootstrapTable($('#carInfoTable'),{
                data:[],
                sidePagination: "client",
                pagination:false,
                singleSelect:false,
                height:130,
                onClickRow:function (row) {
                    isClickingCarData = row;
                    var result = $.ArrayUtils.find(_this.oldCarInfoIds,function (item) {
                        return  item == row.id;
                    });
                    if(!result){
                        _this.oldCarInfoIds.push(row.id);
                        _this.getCarInfo(row,true);
                    }else{
                        // var _index = $.ArrayUtils.findIndex(_this.oldCarInfoIds,function (item) {
                        //     return  item == row.id;
                        // });
                        // if(_index >-1){
                        //     _this.oldCarInfoIds.splice(_index,1);
                        // }
                        // _this.myMap.removeMarkers(row.id);
                    }
                },
                onUncheck:function (row) {
                    var _index = $.ArrayUtils.findIndex(_this.oldCarInfoIds,function (item) {
                        return  item == row.id;
                    });
                    if(_index >-1){
                        _this.oldCarInfoIds.splice(_index,1);
                    }
                    _this.myMap.removeMarkers(row.id);
                },
                columns: [{
                    checkbox: true
                },{
                    field:"id",
                    title:"关注",
                    formatter:function (value,data,index) {
                        if(data.iswatch === true){
                            return "<a style='color:#0ae;cursor: pointer' id='"+value+"' class='watch-box' iswatch='true'><img style='width: 22px;height:22px' src='images/page1/watch.png'></a>"
                        }else{
                            return "<a style='color:#0ae;cursor: pointer' id='"+value+"' class='watch-box' iswatch='false'><img style='width: 22px;height:22px' src='images/page3/u1289.png'></a>"
                        }
                    }
                },{
                    field:"id",
                    title:"图标",
                    formatter:function (value,data,index) {
                        var _treeData = data;
                        var r = "";
                        if(_treeData["warningTypes"] == 0){
                            r = "<img src='images/page3/round-black.png' style='width: 20px;height: 20px'>";
                        }else if(_treeData["warningTypes"] == 1){
                            r = "<img src='images/page3/round-blue.png' style='width:20px;height:20px'>";
                        }
                        else if(_treeData["warningTypes"] == 2){
                            r = "<img src='images/page3/round-red.png' style='width: 20px;height: 20px'>";
                        }
                        else if(_treeData["warningTypes"] == 3){
                            r = "<img src='images/page3/round-red.png' style='width: 20px;height: 20px'>";
                        }
                        else if(_treeData["warningTypes"] == 5){
                            r = "<img src='images/page3/round-red.png' style='width: 20px;height: 20px'>";
                        }
                        return r;
                    }
                },
                    {
                        field: 'baseCarNo',
                        title: '车牌号'
                    }, {
                        field: 'terminal',
                        title: '设备编号'
                    },
                    //     {
                    //     field:"carType",
                    //     title:"设备类型"
                    // },
                    {
                        field: 'gpsDate',
                        title: '定位时间'
                    } ,
                    /*{
                     field: 'onlinetime',
                     title: '停车地点',
                     formatter:function (data) {
                     return "";
                     }
                     }, {
                     field: 'oneHigh',
                     title: '停车时长',
                     formatter:function (value,e,index) {
                     var gpsPowerStatus = e.gpsPowerStatus;
                     var acc = e.accFlag;
                     var oneHigh = e.oneHigh;
                     var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                     var str=""
                     if(gpsPowerStatus == 1){
                     str="主电正常, "
                     }else{
                     str="主电异常, "
                     }
                     if (acc == 1) {
                     str = str+"点火, "
                     }else{
                     str = str+"熄火, "
                     }
                     if(oneHigh){
                     str=str+" 时长: "+sc
                     }else{
                     str=str+" 时长: 0 分钟"
                     }
                     return str
                     }
                     },*/


                    {
                        field: 'onlinetime',
                        title: '通信时间'
                    }, {
                        field: 'deptName',
                        title: '所属用户'
                    }, {
                        field: 'gps',
                        title: '定位状态',
                        formatter:function (value,row,index) {
                            var e = row;
                            //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                            var gpsDate = e.gpsDate;
                            var onlinetime = e.onlinetime;
                            var result = "是";
                            if(gpsDate && onlinetime){
                                var totalMilliseconds = Math.abs(Date.parse(new Date(gpsDate)) - Date.parse(new Date(onlinetime)));
                                var totalMinutes=parseInt(totalMilliseconds / 1000 / 60);
                                if(totalMinutes>2){
                                    result="否"
                                }
                            }else{
                                result="否"
                            }
                            return result
                        }
                    },
                    {
                        field: 'addressDetail',
                        title: '位置'
                    },
                    /*{
                     field: 'status',
                     title: '激活状态',
                     formatter:function (value,row,index) {
                     var e = row;
                     //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                     var gpsPowerStatus = e.gpsPowerStatus;
                     var acc = e.accFlag;
                     var oneHigh = e.oneHigh;
                     var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                     var str=""
                     if(gpsPowerStatus == 1){
                     str="主电正常, "
                     }else{
                     str="主电异常, "
                     }
                     if (acc == 1) {
                     str = str+"点火, "
                     }else{
                     str = str+"熄火, "
                     }
                     if(oneHigh){
                     str=str+" 时长: "+sc
                     }else{
                     str=str+" 时长: 0 分钟"
                     }
                     return str
                     }
                     },*/
                    {
                        field: 'speed',
                        title: '速度'
                    }, {
                        field: 'baojing',
                        title: '方向',
                        formatter:function (value,row,index) {
                            var e = row;
                            //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                            var driection = e.driection;
                            if(driection){
                                return $.mapMethod.queryDriection(driection);
                            }else{
                                return "北"
                            }
                        }
                    },{
                        field: 'daydis',
                        title: '日里程',
                    },{
                        field: 'totalDis',
                        title: '总里程',
                    },
                    /*{
                        field: 'monthDis',
                        title: '温度',
                        formatter:function (value,row,index) {
                            return ""
                        }
                    },
                    {
                        field: 'monthDis',
                        title: '当日油耗',
                        formatter:function (value,row,index) {
                            return ""
                        }
                    },
                    {
                        field: 'monthDis',
                        title: '总油耗',
                        formatter:function (value,row,index) {
                            return ""
                        }
                    }*/
                    //     {
                    //     field: 'Level',
                    //     title: '操作'
                    // }
                ]
                // columns: [{
                //     checkbox: true
                // }, {
                //     field: 'name',
                //     title: '关注'
                // }, {
                //     field: 'deptName',
                //     title: '设备名称'
                // }, {
                //     field: 'carType',
                //     title: '设备类型'
                // } , {
                //     field: 'baseCarNo',
                //     title: '设备ID'
                // }, {
                //     field: 'sim',
                //     title: 'SIM卡号'
                // }, {
                //     field: 'stopDate',
                //     title: '停车时间'
                // }, {
                //     field: 'stopLocation',
                //     title: '停车地点'
                // }, {
                //     field: 'stopTime',
                //     title: '停车时长'
                // }, {
                //     field: 'baojing',
                //     title: '报警'
                // }, {
                //     field: 'Level',
                //     title: '操作'
                // }]

            });
            this.carInfoTable.init();
        },
        _doSearchByDevice:function () {

        },
        _searchByTabCode:function (tabCode) {
            var _this = this;
            $.ajax({
                url:_this.queryCarByStatus_url,
                data:{"tabCode":tabCode},
                type:"GET",
                dataType:"JSON"
            }).done(function (result) {
                if(result){
                    for(var i=0;i<result.length;i++){
                        _this.myMap.addMarkIcon(result[i]);
                    }
                }
            }).fail(function (error) {
            })
        },
        getListCarCount:function () {
            var _this = this;
            $.ajax({
                url:_this.queryCarCount_url,
                type:"GET",
                dataType:"JSON"
            }).done(function (result) {
                if(result.isSuccess == true){
                    // $(".allCar").text(result.data.totalNum);
                    // $(".onCar").text(result.data.onlineNum);
                    // $(".upCar").text(result.data.offlineNum);
                    // $(".runNum").text(result.data.speedNum);
                    // $(".stopNum").text(result.data.stopNum);
                    // $(".speedRate").text(result.data.rate);
                }
            }).fail(function (error) {
            })
        },
        getListMainCarCount:function () {
            var _this = this;
            // $.post("/monitor/queryZDJKCarIds",null,function (_result) {
            //     if(_result.isSuccess){
            //         var watchCarIds =_result.data;
            //         $.ajax({
            //             url:_this.queryCarCountMain_url,
            //             data:{"allCarIds":watchCarIds},
            //             type:"GET",
            //             dataType:"JSON"
            //         }).done(function (result) {
            //             if(result.isSuccess == true){
            //                 $(".totalNum").text(result.total);
            //                 $(".wxNum").text(result.wxnum);
            //                 $(".yxNum").text(result.yxnum);
            //             }
            //         }).fail(function (error) {
            //         })
            //     }else{
            //
            //     }
            // },"json");

        },
        getCarInfo:function (data,isTable,isInterval) {
            var _carIds = [];
            if(isInterval){
                for(var m=0;m<this.oldCarInfoIds.length;m++){
                    this.myMap.removeMarkers(this.oldCarInfoIds[m]);
                }
                _carIds = this.oldCarInfoIds;
            }else{
                var _isCar = false;
                var childrenNodes = this.carInfoTree.getChildrenNodes(data.nodeId);
                if(data.isCar){
                    _isCar = true;
                    _carIds.push(data.id);
                }
                if(childrenNodes && childrenNodes.length){
                    _isCar = true;
                    for(var k=0,
                            _len = childrenNodes.length;k<_len;k++){
                        var children = childrenNodes[k];
                        if(children.isCar){
                            _carIds.push(children.id);
                        }
                    }
                }
                if(!_isCar && !isTable){
                    return false;
                }
                if(!isTable){
                    for(var j = 0;j<_carIds.length;j++){
                        var _carId = _carIds[j];
                        var result = $.ArrayUtils.find(this.oldCarInfoIds,function (item) {
                            return  item == _carId;
                        });
                        if(!result){
                            this.oldCarInfoIds.push(_carId);
                        }
                    }
                }
                if(isTable){
                    _carIds = data.id;
                }

            }
            var _this = this;
            $.ajax({
                url:this.queryTreeNodeInfo,
                data:{"carIds":_carIds.toString()},
                type:"POST",
                dataType:"JSON"
            }).done(function (result) {
                if (result.isSuccess == true) {
                    var dataArr = [];
                    var resData = result.data;
                    $.each(resData, function (key, value) {
                        _this.myMap.addMarkIcon(value);
                        var r = $.ArrayUtils.find(_this.carInfoData,function (item) {
                            return item.id == value.id;
                        });
                        if(!r){
                            dataArr.push(value);
                        }else if(r){
                            // var _index = $.ArrayUtils.findIndex(_this.carInfoData,function (item) {
                            //     return item.id = value.id
                            // });
                            // if(_index > -1){
                            //     _this.carInfoData[_index] = value;
                            // }
                        }
                    });
                    _this.carInfoData = _this.carInfoData.concat(dataArr);
                    _this.carInfoTable.load(_this.carInfoData);
                    for(var j=0,
                            _carLen = _this.carInfoData.length;j<_carLen;j++){
                        _this.carInfoTable.check(j);
                    }


                    // for(var j=0,
                    //         _carLen = _carIds.length;j<_carLen;j++){
                    //     var _carId = _carIds[j];
                    //     var _index = $.ArrayUtils.findIndex(_this.carInfoData,function (item) {
                    //         return item.id = _carId
                    //     });
                    //     _this.carInfoTable.check(_index);
                    // }
                }
            }).fail(function (error) {

            })
        },
        removeCarMarker:function (data) {
            var _this = this;
            var _carIds = [];
            var _isCar = false;
            var childrenNodes = _this.carInfoTree.getChildrenNodes(data.nodeId);
            if(data.isCar){
                _isCar = true;
                _carIds.push(data.id);
            }
            if(childrenNodes && childrenNodes.length){
                _isCar = true;
                for(var k=0,
                        _len = childrenNodes.length;k<_len;k++){
                    var children = childrenNodes[k];
                    if(children.isCar){
                        _carIds.push(children.id);
                    }
                }
            }
            if(!_isCar){
                return false
            }
            for(var kk=0,_len=_carIds.length;kk<_len;kk++){
                var _carId = _carIds[kk];
                var _index = $.ArrayUtils.findIndex(this.oldCarInfoIds,function (item) {
                    return  item == _carId
                });
                if(_index >-1){
                    this.oldCarInfoIds.splice(_index,1);
                }
                var _carIndex = $.ArrayUtils.findIndex(this.carInfoData,function (item) {
                    return item.id == _carId;
                });
                if(_carIndex >-1){
                    this.carInfoData.splice(_carIndex,1);
                }
                _this.myMap.removeMarkers(_carId);
            }
            this.carInfoTable.load(this.carInfoData);
            for(var c=0;c<this.carInfoData.length;c++){
                this.carInfoTable.check(c);
            }
        }
    };

    var monitorView = new MonitorView();
    monitorView.getListCarCount();
    //monitorView.getListMainCarCount();
    monitorView.initBindEvent();
    monitorView.initCarInfoTable();

    // setInterval(function () {
    //     monitorView.getListCarCount();
    //     if(monitorView.oldCarInfoIds.length){
    //         monitorView.getCarInfo(null,null,true);
    //     }
    // },20000);



    $("#carInfoDialog").DialogExt({
        title: '车辆基本信息',
        keyboard: true,
        show: false,
        maxHeight:"300",
        scrollY:true,
        showButton:false,
        onSure: function () {
        }
    });
    $("#guanzhu").on("click",function () {
        var _visible = $(this).attr("data-visible");
        if(_visible == "true"){
            $(this).attr("data-visible","false");
            $("#guanzhu-box-content").animate({
                width:"0px",
                height:"0px",
                opacity: "0",
            },300)
        }else{
            $(this).attr("data-visible","true");
            $("#guanzhu-box-content").animate({
                width:"800px",
                height:"35px",
                opacity: "1",
            },300)
        }

    });
    $(document).bind("contextmenu",function(e){
        return false;
    });
    $(document).bind("click",function(e){
        if($("#rightkey-box").is(":visible")){
            $("#rightkey-box").hide();
        }
    });
    $("#treeview").on("mousedown","li",function (e) {
        var _e = e ? e : window.event;
        _e.preventDefault();
        var isCarDom = $(this).find("span.isCar").length ? true : false;
        if(_e.which==3 && isCarDom){
            var _y = _e.pageY;
            _y = $(".monitor-box").scrollTop() + _y -72;
            $("#rightkey-box").show().css({
                "top":_y+"px",
                "left":"200px"
            })
        }
    });

    $(".hover-box").hover(function () {
        $("#hover-box-ul").show().stop(true);
        return false
    },function () {
        $("#hover-box-ul").hide().stop(true);
        return false
    });
    $("#left-head-tab").on("click","div",function () {
        var _id = $(this).attr("id");
        var $span =$(this).find("span.text");
        // if($span.hasClass("active")){
        //     return false
        // }
        $("#left-head-tab").find("span.text").removeClass("active");
        $span.addClass("active");
        if(_id == "everyCar"){
            monitorView.carInfoTree.reload();
            $("#onlineDialog").hide();
            $("#unonlineDialog").hide();
            $("#carDetailsDialog").hide();
        }else if(_id == "onlineCar"){
            monitorView.carInfoTree.reload({
                online:"1"
            });
            $("#onlineDialog").show();
            $("#unonlineDialog").hide();
            $("#carDetailsDialog").hide();
        }else if(_id == "unOnlineCar"){
            monitorView.carInfoTree.reload({
                online:"0"
            });
            $("#unonlineDialog").show();
            $("#onlineDialog").hide();
            $("#carDetailsDialog").hide();
        }
    });

    // $("#onlineCar").on("click",function () {
    //     $("#onlineDialog").show();
    // });
    $("#onlineClose").on("click",function () {
        $("#onlineDialog").hide();
    });
    // $("#unOnlineCar").on("click",function () {
    //     $("#unonlineDialog").show();
    // });
    $("#unOnlineClose").on("click",function () {
        $("#unonlineDialog").hide();
    });
    $("#unOnTimelineClose").on("click",function () {
        $("#unonTimelineDialog").hide();
    });
    $("#unRunlineClose").on("click",function () {
        $("#norunlineDialog").hide();
    });

    $("#exportONlineCar").click(function () {
        window.location.href = window.serveraddr+"/carHisExcel/exportCarStateExcel?online=1"
    });
    $("#exportUnONlineCar").click(function () {
        window.location.href = window.serveraddr+"/carHisExcel/exportCarStateExcel?online=0&sblxday="+$("#unlineData").text()
    });
    $("#exportZdjkCar").click(function () {
        window.location.href = window.serveraddr+"/carHisExcel/exportCarZDExcel?allCarIds="+zdgzCarIds.toString();
    });
    $("#exportDuandianReport").click(function () {
        window.location.href = window.serveraddr+"/tabWaringInfo/exportExcel?baseCarNo="+$("#baseCarNo").val()+"&waringstate="+$("#waringstate").val()+"&start="+$("#start").val()+"&end="+$("#end").val()+"&deptId="+$("#warningDeptId").val()+"&waringType="+$("#waringtype").val()
    });
    $("#exportNorunlineCar").click(function () {
        window.location.href = window.serveraddr+"/carHisExcel/exportWXSCarExcel?deptName="+$("#norunlineDeptName").val()
    });
    $("#exportUnonTimeline").click(function () {
        window.location.href = window.serveraddr+"/carHisExcel/exportWSXCarExcel?deptName="+$("#unonTimeDeptName").val()
    });
    //$.loadingBar.show("数据加载中...");
    /**
     * 在线列表
     * @type {BootstrapTable}
     */
    var onlineCarInfoTable  = new BootstrapTable($('#onlineCarInfoTable'),{
        data:[],
        //url:"/monitor/listByRealOnLineCar",
        pagination:false,
        singleSelect:false,
        columns: [
            {
                field: 'userName',
                title: '车主姓名'
            },{
            field: 'baseCarNo',
            title: '车牌号'
        }, {
            field: 'terminal',
            title: '设备编号'
        }, {
            field: 'deptName',
            title: '所属用户'
        },
            {
                field: 'gpsDate',
                title: '当前在线时间'
            },
            {
                field: 'oneHigh',
                title: '离线时间',
                formatter:function (e) {
                    var gpsPowerStatus = e.gpsPowerStatus;
                    var acc = e.accFlag;
                    var oneHigh = e.oneHigh;
                    var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                    var str=""
                    if(gpsPowerStatus == 1){
                        str="主电正常, "
                    }else{
                        str="主电异常, "
                    }
                    if (acc == 1) {
                        str = str+"点火, "
                    }else{
                        str = str+"熄火, "
                    }
                    if(oneHigh){
                        str=str+" 时长: "+sc
                    }else{
                        str=str+" 时长: 0 分钟"
                    }
                    return str
                }
            },
            {
                field: 'oneHigh',
                title: '在线状态'
            }
        ]
    });
    onlineCarInfoTable.init();
    /**
     * 离线列表
     * @type {BootstrapTable}
     */
    var unOnlineCarInfoTable  = new BootstrapTable($('#unOnlineCarInfoTable'),{
        data:[],
        //url:"/monitor/listByRealOutLineCar?sblxday="+$("#unlineData").text(),
        pagination:false,
        singleSelect:false,
        onLoadSuccess:function (data) {
            if(data && data.rows){
                islxCacheData=true;
                var _rows = data.rows.length;
                var _carLen = window.localStorage.getItem("lxCacheData");
                if(_rows != _carLen){
                    $(".upCar").text(data.rows.length);
                    window.localStorage.setItem("lxCacheData",_rows);
                }
            }
        },
        columns: [{
            checkbox: true
        },
            {
                field: 'userName',
                title: '车主姓名'
            },{
            field: 'baseCarNo',
            title: '车牌号',
                formatter:function (value,row,index) {
                    return "<a style='color: #0ae;cursor: pointer' class='openMonitor' carid='"+row.id+"' carno='"+row.baseCarNo+"'>"+value+"</a>"
                }
        }, {
            field: 'terminal',
            title: '设备编号'
        }, {
            field: 'deptName',
            title: '所属用户'
        }, {
            field: 'rate',
            title: '回传频率',
                formatter:function (value,row,index) {
                    var carObj = row;
                    var _rate = "";
                    if(carObj.protocolType ==1){
                        if (carObj.accFlag == "1") {
                            _rate = 30+"s"
                        }else {
                            _rate = 60+"s"
                        }
                    }else{
                        _rate = 24+"h";
                    }
                    // var str = value+"h";
                    return _rate
                }
        },
            {
                field: 'gpsDate',
                title: '最后在线时间'
            },
            {
                field: 'oneHigh',
                title: '离线时长',
                formatter:function (value,row,index) {
                    var e = row;
                    var gpsPowerStatus = e.gpsPowerStatus;
                    var acc = e.accFlag;
                    var oneHigh = e.oneHigh;
                    var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                    var str=""
                    if(gpsPowerStatus == 1){
                        str="主电正常, "
                    }else{
                        str="主电异常, "
                    }
                    if (acc == 1) {
                        str = str+"点火, "
                    }else{
                        str = str+"熄火, "
                    }
                    if(oneHigh){
                        // str=str+" 时长: "+sc
                        str=" 时长: "+sc
                    }else{
                        // str=str+" 时长: 0 分钟"
                        str=" 时长: 0 分钟"
                    }
                    return str
                }
            }
        ]
    });
    unOnlineCarInfoTable.init();
    $("#searchUnlineDataBtn").click(function () {
        unOnlineCarInfoTable.refresh({
            query:{
                //sblxday:$("#unlineData").text(),
                deptName:$("#unLineDeptName").val()
            }
        })
    });
    $("#searchUnTimeDataBtn").click(function () {
        unOnTimelineCarInfoTable.refresh({
            query:{
                deptName:$("#unonTimeDeptName").val()
            }
        })
    });
    /**
     * 未运行列表
     * @type {BootstrapTable}
     */
    var norunlineInfoTable  = new BootstrapTable($('#norunlineInfoTable'),{
        data:[],
        //url:"/monitor/listByWXSCar?wxsday="+$("#noneRun").text(),
        pagination:false,
        singleSelect:false,
        onLoadSuccess:function (data) {
            if(data && data.rows){
                var _rows = data.rows.length;
                var _count = window.localStorage.getItem("norunlineCacheData");
                if(_rows != _count){
                    $(".deviceUnRun").text(data.rows.length);
                    window.localStorage.setItem("norunlineCacheData",_rows)
                }
                _deviceUnrun_data=true;
            }
        },
        columns: [{
            checkbox: true
        },
            {
                field: 'userName',
                title: '车主姓名'
            },{
            field: 'baseCarNo',
            title: '车牌号',
                formatter:function (value,row,index) {
                    return "<a style='color: #0ae;cursor: pointer' class='openMonitor' carid='"+row.id+"' carno='"+row.baseCarNo+"'>"+value+"</a>"
                }
        }, {
            field: 'terminal',
            title: '设备编号'
        }, {
            field: 'deptName',
            title: '所属用户'
        },{
            field: 'accFlag',
            title: '运行状态',
            formatter:function (value,row,index) {
                // var acc = row.accFlag;
                // if (acc == 1) {
                //     return "运行"
                // }else{
                //     return "停止"
                // }
                if (row.onlineName == "0") {
                    return "离线"
                }else if (row.onlineName == "1" && parseInt(row.speed) <10 ) {
                    return "停止"
                } else if (row.onlineName == "1" && parseInt(row.speed) >= 10) {
                    return "运行"
                }else if(row.warningTypes == "3" || row.warningTypes == "5" || row.warningTypes == "8"){
                    return "异常"
                }

            }
        } ,
            {
                field: 'gpsDate',
                title: '停车时间',
                formatter:function (value,row,index) {
                    var e = row;
                    var acc = row.accFlag;
                    // if (acc == 1) {
                    //     return ""
                    // }
                    if (row.onlineName == "1" && parseInt(row.speed) <10 ) {
                        var gpsDate = e.gpsDate;
                        var oneHigh = e.oneHigh;
                        if(gpsDate && oneHigh){
                            var date1 = new Date(gpsDate);

                            var s1 = date1.getTime()- oneHigh*60*1000;
                            var date2 = new Date(s1);
                            var str="";
                            return date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + date2.getDate()+ ' ' + date2.getHours()+":"+ date2.getMinutes()+":"+ date2.getSeconds();
                        }else{
                            return ""
                        }
                    }else{
                        return "";
                    }


                }
            },
            {
                field: 'oneHigh',
                title: '停车时长',
                // formatter:function (value,row,index) {
                //     var e = row;
                //     var gpsPowerStatus = e.gpsPowerStatus;
                //     var acc = e.accFlag;
                //     var oneHigh = e.oneHigh;
                //     var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                //     var str=""
                //     if(gpsPowerStatus == 1){
                //         str="主电正常, "
                //     }else{
                //         str="主电异常, "
                //     }
                //     if (acc == 1) {
                //         str = str+"点火, "
                //     }else{
                //         str = str+"熄火, "
                //     }
                //     if(oneHigh){
                //         // str=str+" 时长: "+sc
                //         str=" 时长: "+sc
                //     }else{
                //         // str=str+" 时长: 0 分钟"
                //         str=" 时长: 0 分钟"
                //     }
                //     return str
                // }
                formatter:function (value,row,index) {
                    var e = row;

                    var gpsPowerStatus = e.gpsPowerStatus;
                    var acc = e.accFlag;
                    // if (acc == 1) {
                    //     return ""
                    // }
                    if (row.onlineName == "1" && parseInt(row.speed) <10 ){
                        var oneHigh = e.oneHigh;
                        var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                        var str="";
                        // if(gpsPowerStatus == 1){
                        //     str="主电正常, "
                        // }else{
                        //     str="主电异常, "
                        // }
                        // if (acc == 1) {
                        //     str = str+"点火, "
                        // }else{
                        //     str = str+"熄火, "
                        // }
                        if(oneHigh){
                            str=str+" 时长: "+sc
                        }else{
                            str=str+" 时长: 0 分钟"
                        }
                        return str
                    }else{
                        return ""
                    }

                }
            }
        ]
    });
    norunlineInfoTable.init();
    $("#searchNoRunDataBtn").click(function () {
        norunlineInfoTable.refresh({
            deptName:$("#norunlineDeptName").val()
        })
    });
    // $("#searchNoTimeBtn").click(function () {
    //     warningInfoTable.refresh({
    //         start:$("#start").val(),
    //         end:$("#end").val()
    //     })
    // });
    $('#guanzhuInfoTable').on("dblclick","a.openMonitor",function () {
        var _carId = $(this).attr("carid");
        var _carno = $(this).attr("carno");
        window.location.href="/monitor?carId="+_carId+"&baseCarNo="+_carno;
    });
    $('#unOnlineCarInfoTable').on("dblclick","a.openMonitor",function () {
        var _carId = $(this).attr("carid");
        var _carno = $(this).attr("carno");
        window.location.href="/monitor?carId="+_carId+"&baseCarNo="+_carno;
    });
    $('#unOnTimelineCarInfoTable').on("dblclick","a.openMonitor",function () {
        var _carId = $(this).attr("carid");
        var _carno = $(this).attr("carno");
        window.location.href="/monitor?carId="+_carId+"&baseCarNo="+_carno;
    });
    $('#norunlineInfoTable').on("dblclick","a.openMonitor",function () {
        var _carId = $(this).attr("carid");
        var _carno = $(this).attr("carno");
        window.location.href="/monitor?carId="+_carId+"&baseCarNo="+_carno;
    });
    $('#duandianTable').on("dblclick","a.openMonitor",function () {
        var _carId = $(this).attr("carid");
        var _carno = $(this).attr("carno");
        window.location.href="/monitor?carId="+_carId+"&baseCarNo="+_carno;
    });
    /**
     * 关注列表
     * @type {BootstrapTable}
     */
    var guanzhuInfoTable  = new BootstrapTable($('#guanzhuInfoTable'),{
        data:[],
        sidePagination: "client",
        pagination:false,
        singleSelect:false,
        columns: [
            {
                field: 'userName',
                title: '车主姓名'
            },
            {
                field: 'baseCarNo',
                title: '车牌号',
                formatter:function (value,row,index) {
                    return "<a style='color: #0ae;cursor: pointer' class='openMonitor' carid='"+row.id+"' carno='"+row.baseCarNo+"'>"+value+"</a>"
                }
            },
            // {
            //     field: 'deptName',
            //     title: '所属客户'
            // },
            // {
            //     field: 'terminal',
            //     title: '设备名称'
            // },
            {
                field: 'carTypes',
                title: '车辆类型'
            },
            {
                field: 'terminal',
                title: '设备编号'
            },
            {
                field: 'sim',
                title: 'SIM卡号'
            },
            {
                field: 'accFlag',
                title: '运行状态',
                formatter:function (value,row,index) {
                    // var acc = row.accFlag;
                    // if (acc == 1) {
                    //     return "运行"
                    // }else{
                    //     return "停止"
                    // }
                    if (row.onlineName == "0") {
                        return "离线"
                    }else if (row.onlineName == "1" && parseInt(row.speed) <10 ) {
                        return "停止"
                    } else if (row.onlineName == "1" && parseInt(row.speed) >= 10) {
                        return "运行"
                    }else if(row.warningTypes == "3" || row.warningTypes == "5" || row.warningTypes == "8"){
                        return "异常"
                    }
                }
            } ,
            {
                field: 'gpsDate',
                title: '停车时间',
                formatter:function (value,row,index) {
                    var e = row;
                    var acc = row.accFlag;
                    // if (acc == 1) {
                    //     return ""
                    // }
                    if (row.onlineName == "1" && parseInt(row.speed) <10 ) {
                        var gpsDate = e.gpsDate;
                        var oneHigh = e.oneHigh;
                        if(gpsDate && oneHigh){
                            var date1 = new Date(gpsDate);

                            var s1 = date1.getTime()- oneHigh*60*1000;
                            var date2 = new Date(s1);
                            var str="";
                            return date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + date2.getDate()+ ' ' + date2.getHours()+":"+ date2.getMinutes()+":"+ date2.getSeconds();
                        }else{
                            return ""
                        }
                    }else{
                        return "";
                    }


                }
            } ,
            {
                field: 'addressDetail',
                title: '停车地点',
                formatter:function (value,row,index) {
                    var e = row;
                    var acc = row.accFlag;
                    // if (acc == 1) {
                    //     return ""
                    // }
                    if (row.onlineName == "1" && parseInt(row.speed) <10 ) {
                        return value
                    }else{
                        return "";
                    }


                }
            }, {
                field: 'oneHigh',
                title: '停车时长',
                formatter:function (value,row,index) {
                    var e = row;

                    var gpsPowerStatus = e.gpsPowerStatus;
                    var acc = e.accFlag;
                    // if (acc == 1) {
                    //     return ""
                    // }
                    if (row.onlineName == "1" && parseInt(row.speed) <10 ){
                        var oneHigh = e.oneHigh;
                        var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                        var str="";
                        // if(gpsPowerStatus == 1){
                        //     str="主电正常, "
                        // }else{
                        //     str="主电异常, "
                        // }
                        // if (acc == 1) {
                        //     str = str+"点火, "
                        // }else{
                        //     str = str+"熄火, "
                        // }
                        if(oneHigh){
                            str=str+" 时长: "+sc
                        }else{
                            str=str+" 时长: 0 分钟"
                        }
                        return str
                    }else{
                        return ""
                    }

                }
            },
//             {
//                 field: 'warningType',
//                 title: '报警',
//                 formatter:function (value,row,index) {
//                     //e是得到的json数组中的一个item ，可以用于控制标签的属性。
// //                    debugger;
//                     var e = row;
//                     var d = e.warningType;
// //                    1-驶入  2-驶出 3-超速 4-路线偏离 5-停车超时报警  6-疲劳驾驶报警 7-凌晨行车报警
//                     var result = "";
//                     if (d == 1) {
//                         result = '驶入报警'
//                     } else if (d == 2) {
//                         result = '驶出报警'
//                     } else if (d == 3) {
//                         result = '超速报警'
//                     } else if (d == 4) {
//                         result = '路线偏离报警'
//                     } else if (d == 5) {
//                         result = '停车超时报警'
//                     } else if (d == 6) {
//                         result = '疲劳驾驶报警'
//                     } else if (d == 7) {
//                         result = '凌晨行车报警'
//                     }
//                     return result
//                 }
//             },
            {
                field: 'id',
                title: '操作',
                width: 150,
                formatter:function (value,row,index) {
                    return "<span style='margin-right: 10px'><a  class='openCar warningOpration' rowid='"+row.id+"'>车辆</a></span>"
                        +"<span style='margin-right: 10px'><a class='openHis warningOpration' rowid='"+row.id+"' rowcarno='"+row.baseCarNo+"'>轨迹</a></span>"
                        +"<a  class='cancelWatch warningOpration'  iswatch='false' rowid='"+row.id+"'>取消关注</a>"
                }
            }
            // {
            //     field: 'onlinetime',
            //     title: '通信时间'
            // },
            // {
            //     field: 'deptName',
            //     title: '所属客户'
            // },
            // {
            //     field: 'gps',
            //     title: '定位',
            //     formatter:function (value,row,index) {
            //         var e = row;
            //         //e是得到的json数组中的一个item ，可以用于控制标签的属性。
            //         var gpsDate = e.gpsDate;
            //         var onlinetime = e.onlinetime;
            //         var result = "是";
            //         if(gpsDate && onlinetime){
            //             var totalMilliseconds = Math.abs(Date.parse(new Date(gpsDate)) - Date.parse(new Date(onlinetime)));
            //             var totalMinutes=parseInt(totalMilliseconds / 1000 / 60);
            //             if(totalMinutes>2){
            //                 result="否"
            //             }
            //         }else{
            //             result="否"
            //         }
            //         return result
            //     }
            // },
            // {
            //     field: 'status',
            //     title: '状态',
            //     formatter:function (value,row,index) {
            //         var e = row;
            //         //e是得到的json数组中的一个item ，可以用于控制标签的属性。
            //         var gpsPowerStatus = e.gpsPowerStatus;
            //         var acc = e.accFlag;
            //         var oneHigh = e.oneHigh;
            //         var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
            //         var str=""
            //         if(gpsPowerStatus == 1){
            //             str="主电正常, "
            //         }else{
            //             str="主电异常, "
            //         }
            //         if (acc == 1) {
            //             str = str+"点火, "
            //         }else{
            //             str = str+"熄火, "
            //         }
            //         if(oneHigh){
            //             str=str+" 时长: "+sc
            //         }else{
            //             str=str+" 时长: 0 分钟"
            //         }
            //         return str
            //     }
            // },
            // {
            //     field: 'speed',
            //     title: '速度'
            // },
            // {
            //     field: 'baojing',
            //     title: '方向',
            //     formatter:function (value,row,index) {
            //         var e = row;
            //         //e是得到的json数组中的一个item ，可以用于控制标签的属性。
            //         var driection = e.driection;
            //         if(driection){
            //             return $.mapMethod.queryDriection(driection);
            //         }else{
            //             return "北"
            //         }
            //     }
            // },
            // {
            //     field: 'daydis',
            //     title: '日里程',
            // },
            // {
            //     field: 'monthDis',
            //     title: '总里程',
            // }
        ]
    });
    guanzhuInfoTable.init();

    var warningInfoTable  = new BootstrapTable($('#warningInfoTable'),{
        data:[],
        //url:"/monitor/listByCarWaring",
        pagination:false,
        singleSelect:false,
        columns: [
            {
                field: 'userName',
                title: '车主姓名'
            },
            {
                field: 'baseCarNo',
                title: '车牌号'
            },
            {
                field: 'deptName',
                title: '所属用户'
            },
            {
                field: 'warningType',
                title: '报警类型',
                formatter:function (value,row,index) {
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
//                    debugger;
                    var e = row;
                    var d = e.warningType;
//                    1-驶入  2-驶出 3-超速 4-路线偏离 5-停车超时报警  6-疲劳驾驶报警 7-凌晨行车报警
                    var result = "";
                    if (d == 1) {
                        result = '驶入报警'
                    } else if (d == 2) {
                        result = '驶出报警'
                    } else if (d == 3) {
                        result = '超速报警'
                    } else if (d == 4) {
                        result = '路线偏离报警'
                    } else if (d == 5) {
                        result = '停车超时报警'
                    } else if (d == 6) {
                        result = '疲劳驾驶报警'
                    } else if (d == 7) {
                        result = '凌晨行车报警'
                    }
                    return result
                }
            },
            {
                field: 'waringtime',
                title: '报警时间'
            },
            {
                field: 'status',
                title: '状态',
                formatter:function (value,row,index) {
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
//                    debugger;
//                    accFlag
                    var e = row;
                    var d = e.type;
                    // var result = '<a class="btn btn-info btn-rounded" >已处理</a>'<a class="btn btn-warning btn-rounded" >未处理</a>
                    var result = '已处理';
//                    车牌号颜色[1-4]黄白蓝黑
                    if (d == 1) {
                        result = '已处理'
                    } else if (d == 0) {
                        result = '未处理'
                    }
                    return result
                }
            },
            {
                field: 'sim',
                title: '操作',
                formatter:function (value,row,index) {
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
//                    debugger;
//                    accFlag
                    var e = row;
                    var d = e.type;
                    var result = '';
//                    车牌号颜色[1-4]黄白蓝黑
                    if (d == 1) {
                        result = '';
                    } else if (d == 0) {
                        result = '<a style="color: #0ae" class="handelWarning" warningDataId = "'+row.id+'">处理报警</a>';
                    }
                    return result;
                }
            }
        ]
    });
    warningInfoTable.init();
    var opInfoTable  = new BootstrapTable($('#opInfoTable'),{
        data:[],
        //url:"/monitor/listByCarWaring",
        pagination:false,
        singleSelect:false,
        columns: [
            {
                field: 'userName',
                title: '车主姓名'
            },
            {
                field: 'baseCarNo',
                title: '车牌号'
            },
            // {
            //     field: 'deptName',
            //     title: '所属用户'
            // },
            // {
            //     field: 'terminal',
            //     title: '报警类型'
            // },
            {
                field: 'gpsDate',
                title: '时间'
            },
            // {
            //     field: 'status',
            //     title: '状态',
            //     formatter:function (value,row,index) {
            //         var e = row;
            //         //e是得到的json数组中的一个item ，可以用于控制标签的属性。
            //         var gpsPowerStatus = e.gpsPowerStatus;
            //         var acc = e.accFlag;
            //         var oneHigh = e.oneHigh;
            //         var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
            //         var str=""
            //         if(gpsPowerStatus == 1){
            //             str="主电正常, "
            //         }else{
            //             str="主电异常, "
            //         }
            //         if (acc == 1) {
            //             str = str+"点火, "
            //         }else{
            //             str = str+"熄火, "
            //         }
            //         if(oneHigh){
            //             str=str+" 时长: "+sc
            //         }else{
            //             str=str+" 时长: 0 分钟"
            //         }
            //         return str
            //     }
            // },
            {
                field: 'sim',
                title: '内容',
                formatter:function (value,row,index) {
                    return "";
                }
            }
        ]
    });
    opInfoTable.init();


    var chaijiInfoTable  = new BootstrapTable($('#chaijiInfoTable'),{
        data:[],
        sidePagination: "client",
        pagination:false,
        singleSelect:false,
        columns: [
            {
                field: 'terminal',
                title: '序号'
            },
            {
                field: 'carTypes',
                title: '用户名称'
            },
            {
                field: 'id',
                title: '技工'
            },
            {
                field: 'sim',
                title: '类型'
            },
            {
                field: 'gpsDate',
                title: '操作时间'
            }
        ]
    });
    chaijiInfoTable.init();

    // $.get("/monitor/warningCarTotal",null,function (result) {
    //     $(".baojingNum").text(result.total)
    // });
    var duandianInfoTable  = new BootstrapTable($('#duandianTable'),{
        data:[],
        //url:"/monitor/queryDuanDianReport",
        sidePagination: "server",
        // pagination:true,
        singleSelect:false,
        onLoadSuccess:function (data) {
            // $.loadingBar.hide();
            // debugger;
            //$(".baojingNum").text(data.total);
            var __rows = data.carTotal ? data.carTotal : 0;
            var __count = window.localStorage.getItem("duandianCacheData");
            // if(__rows != __count){
            //     $(".baojingNum").text(data.carTotal ? data.carTotal : 0);
            //     window.localStorage.setItem("duandianCacheData",__rows);
            // }
            $(".baojingNum").text(__rows);
            window.localStorage.setItem("duandianCacheData",__rows);
            _duandian_data=true;

        },
        columns: [
            // {
            //     field: 'terminal',
            //     title: '设备序列号'
            // },
            // {
            //     field: 'carTypes',
            //     title: '分组'
            // },
            {
                field: 'userName',
                title: '车主姓名'
            },
            {
                field: 'baseCarNo',
                title: '车牌号',
                formatter:function (value,row,index) {
                    return "<a style='color: #0ae;cursor: pointer' class='openMonitor' carid='"+row.carID+"' carno='"+row.baseCarNo+"'>"+value+"</a>"
                }
            },
            // {
            //     field: 'userName',
            //     title: '车主名'
            // },
            {
                field: 'warningType',
                title: '报警类型',
                formatter:function (value,row,index) {
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
//                    debugger;
                    var e = row;
                    var d = e.warningType;
//                    1-驶入  2-驶出 3-超速 4-路线偏离 5-停车超时报警  6-疲劳驾驶报警 7-凌晨行车报警
                    var result = "";
                    if (d == 1) {
                        result = '驶入报警'
                    } else if (d == 2) {
                        result = '驶出报警'
                    } else if (d == 3) {
                        result = '超速报警'
                    } else if (d == 4) {
                        result = '路线偏离报警'
                    } else if (d == 5) {
                        result = '停车超时报警'
                    } else if (d == 6) {
                        result = '疲劳驾驶报警'
                    } else if (d == 7) {
                        result = '凌晨行车报警'
                    }else if(d==8){
                        result = '断电报警'
                    }
                    return result
                }
            },
            {
                field: 'waringstate',
                title: '处理状态',
                formatter:function (value,row,index) {
                    var txt = "";
                    switch (parseInt(value)){
                        case 0:
                            txt = "未处理"
                            break;
                        case 1:
                            txt = "已处理"
                            break;

                    };
                    return txt
                }
            },
            {
                field: 'waringtime',
                title: '报警时间'
            },
            {
                field: 'endTime',
                title: '恢复时间'
            },
            {
                field: 'waringcontent',
                title: '报警区域'
            },
            {
                field: 'oneHigh',
                title: '持续时间',
                formatter:function (value,row,index) {
                    if(value){
                        return value+"分钟"
                    }
                    return "";
                }
            },
            {
                field: 'id',
                title: '操作',
                formatter:function (value,row,index) {
                    var _waringstate = row.waringstate;
                    if(parseInt(_waringstate) == 0){
                        return "<a  class='oprationwarning warningOpration' rowid='"+row.id+"' rowtype='"+row.warningType+"'>报警处理</a>";
                    }
                    return ""
                }
            }
            // {
            //     field: 'gpsDate',
            //     title: '操作人'
            // },
            // {
            //     field: 'gpsDate',
            //     title: '操作'
            // }
        ]
    });
    duandianInfoTable.init();
    /**
     * 在线列表
     * @type {BootstrapTable}
     */
    var zhengchangInfoTable  = new BootstrapTable($('#zhengchangInfoTable'),{
        data:[],
        //url:"/monitor/listByRealOnLineCar",
        pagination:false,
        singleSelect:false,
        onLoadSuccess:function (data) {
            var _total = data.total ? data.total : 0;
            var _count = window.localStorage.getItem("zhengchangCacheData");
            if(_total != _count){
                window.localStorage.setItem("zhengchangCacheData",_total);
                $(".zhengcahngCarNum").text(data.total);
            }
            _zhengcahngCarNum=true;
        },
        columns: [
            {
                field: 'userName',
                title: '车主姓名'
            },{
            field: 'baseCarNo',
            title: '车牌号'
        }, {
            field: 'terminal',
            title: '设备编号'
        }, {
            field: 'deptName',
            title: '所属用户'
        },
            {
                field: 'gpsDate',
                title: '当前在线时间'
            }
            // ,
            // {
            //     field: 'oneHigh',
            //     title: '离线时间',
            //     formatter:function (value,row,index) {
            //         var e = row;
            //         var gpsPowerStatus = e.gpsPowerStatus;
            //         var acc = e.accFlag;
            //         var oneHigh = e.oneHigh;
            //         var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
            //         var str=""
            //         // if(gpsPowerStatus == 1){
            //         //     str="主电正常, "
            //         // }else{
            //         //     str="主电异常, "
            //         // }
            //         // if (acc == 1) {
            //         //     str = str+"点火, "
            //         // }else{
            //         //     str = str+"熄火, "
            //         // }
            //         if(oneHigh){
            //             str=str+" 时长: "+sc
            //         }else{
            //             str=str+" 时长: 0 分钟"
            //         }
            //         return str
            //     }
            // },
            // {
            //     field: 'oneHigh',
            //     title: '在线状态'
            // }
        ]
    });
    zhengchangInfoTable.init();
    // $.ajax({
    //     url:"/monitor/updateWarningType",
    //     type:"POST",
    //     data:{"id":1686},
    //     dataType:"JSON"
    // }).done(function (result) {
    //     debugger;
    // });
    var unOnTimelineCarInfoTable  = new BootstrapTable($('#unOnTimelineCarInfoTable'),{
        data:[],
        //url:"/monitor/listByWSXCar?sblxwxday="+$("#unlineDeviceData").text(),
        pagination:false,
        singleSelect:false,
        onLoadSuccess:function (data) {
            if(data && data.rows){
                var _rows = data.rows.length;
                var __count = window.localStorage.getItem("deviceCacheData");
                if(_rows != __count){
                    $(".deviceUnLine").text(_rows);
                    window.localStorage.setItem("deviceCacheData",_rows)
                }
            }
        },
        columns: [
            {
            checkbox: true
        },
            {
                field: 'userName',
                title: '车主姓名'
            },{
            field: 'baseCarNo',
            title: '车牌号',
                formatter:function (value,row,index) {
                    return "<a style='color: #0ae;cursor: pointer' class='openMonitor' carid='"+row.id+"' carno='"+row.baseCarNo+"'>"+value+"</a>"
                }
        }, {
            field: 'terminal',
            title: '设备编号'
        }, {
            field: 'deptName',
            title: '所属用户'
        }, {
            field: 'rate',
            title: '回传频率',
                formatter:function (value,row,index) {
                    var carObj = row;
                    var _rate = "";
                    if(carObj.protocolType ==1){
                        if (carObj.accFlag == "1") {
                            _rate = 30+"s"
                        }else {
                            _rate = 60+"s"
                        }
                    }else{
                        _rate = 24+"h";
                    }
                    // var str = value+"h";
                    return _rate
                }
        },
            {
                field: 'gpsDate',
                title: '最后在线时间'
            },
            {
                field: 'oneHigh',
                title: '离线时长',
                formatter:function (value,row,index) {
                    var e = row;
                    var gpsPowerStatus = e.gpsPowerStatus;
                    var acc = e.accFlag;
                    var oneHigh = e.oneHigh;
                    var sc=Math.floor(oneHigh/60) + "小时" + Math.floor(oneHigh%60) + "分" ;
                    var str=""
                    if(gpsPowerStatus == 1){
                        str="主电正常, "
                    }else{
                        str="主电异常, "
                    }
                    if (acc == 1) {
                        str = str+"点火, "
                    }else{
                        str = str+"熄火, "
                    }
                    if(oneHigh){
                        // str=str+" 时长: "+sc
                        str=" 时长: "+sc
                    }else{
                        // str=str+" 时长: 0 分钟"
                        str=" 时长: 0 分钟"
                    }
                    return str
                }
            }
        ]
    });
    unOnTimelineCarInfoTable.init();
    $('#duandianTable').on("click","a.oprationwarning",function () {
        var id = $(this).attr("rowid");
        var _warningDataId = $(this).attr("rowtype");
        $.ajax({
            url:"/map/warningCl",
            data:{"id":id,"warningType":_warningDataId},
            dataType:"JSON",
            type:"GET"
        }).done(function (result) {
            duandianInfoTable.refresh({
                query:{
                    baseCarNo:$("#baseCarNo").val(),
                    waringstate:$("#waringstate").val(),
                    start:$("#start").val(),
                    end:$("#end").val(),
                    deptId:$("#warningDeptId").val(),
                    waringType:$("#waringtype").val()
                }
            });
        })
    });



    $('#warningInfoTable').on("click","a.handelWarning",function () {
        var _warningDataId = $(this).attr("warningDataId");
        $.ajax({
            url:"/monitor/updateWarningType",
            data:{"id":_warningDataId},
            dataType:"JSON"
        }).done(function (result) {
            //debugger;
        })
    });
    $('#guanzhuInfoTable').on("click","a.handelWarning",function () {
        var _warningDataId = $(this).attr("warningDataId");
        $.ajax({
            url:"/monitor/updateWarningType",
            data:{"id":_warningDataId},
            dataType:"JSON"
        }).done(function (result) {
            //debugger;
        })
    });
    $('#guanzhuInfoTable').on("click","a.cancelWatch",function () {
        if(isSubmitData){
            return false;
        }
        var _rowId = $(this).attr("rowid");
        //var _watchCarIds = window.localStorage.getItem("localguanzhuData");
       // _watchCarIds = _watchCarIds.concat(guanzhuIds);
        //window.localStorage.setItem("localguanzhuData",_watchCarIds);
        // if(_watchCarIds && typeof _watchCarIds == "string"){
        //     _watchCarIds = _watchCarIds.split(",");
        // }
        if(zdgzCarIds && zdgzCarIds.length){
            var _index = $.ArrayUtils.findIndex(zdgzCarIds,function (item) {
                return  item == _rowId
            });
            //watchCars.splice(_index,1);
            zdgzCarIds.splice(parseInt(_index),1);
            // window.localStorage.removeItem("localguanzhuData");
            // window.localStorage.setItem("localguanzhuData",_watchCarIds);
            // reloadGuanzhuInfoTable();
            isSubmitData = true;
            $.post("/monitor/makeZDJKCarIds",{carIds:zdgzCarIds.toString()},function (result) {
                isSubmitData = false;
                if(result.isSuccess){
                    queryGuanzhuInfoTable(true);
                }else{
                    $.messageExt.alert("系统提示","取消重点关注失败！","error");
                }
            },"json");
           // makeZDGZfn(zdgzCarIds);
        }

    });
    $('#guanzhuInfoTable').on("click","a.openHis",function () {
            var _rowId = $(this).attr("rowid");
            var _rowcarno = $(this).attr("rowcarno");
            window.open("/map/his?carId="+_rowId+"&carNo="+_rowcarno);
    });
    $('#guanzhuInfoTable').on("click","a.openCar",function () {
        var _rowId = $(this).attr("rowid");
        var _rowcarno = $(this).attr("rowcarno");
        _showCarInfo(0,_rowId)
    });
    $("#carInfoTable").on("click","a.watch-box",function () {
        var _state = $(this).attr("iswatch");
        var old_index = $.ArrayUtils.findIndex(monitorView.carInfoData,function (item) {
            if(isClickingCarData){
                return  item.id == isClickingCarData.id;
            }
            return false
        });
        if(_state == "true"){
            var _index = $.ArrayUtils.findIndex(watchCars,function (item) {
                if(isClickingCarData){
                    return  item.id == isClickingCarData.id;
                }
                return false
            });
            watchCars.splice(_index,1);
            watchCarIds.splice(_index,1);
            monitorView.carInfoData[old_index]["iswatch"] = false;
            $(this).find("img").attr("src","images/page3/u1289.png");
            if(watchCars.length<=0){
                $("#guanzhu-btn").find("span.box-img").css({"opacity":"1"});
                $("#guanzhu-btn").find("span.text").text("关注").css({"color":"#333"});
                $("#guanzhu-num").css({"display":"none"});
                $("#guanzhu-num").find("em").text(0);
            }
        }else{
            $(this).attr("iswatch","true");
            monitorView.carInfoData[old_index]["iswatch"] = true;
            watchCars.push(isClickingCarData);
            watchCarIds.push(isClickingCarData.id);
            $(this).find("img").attr("src","images/page1/watch.png");
            if(watchCars.length){
                $("#guanzhu-btn").find("span.box-img").css({"opacity":"0"});
                $("#guanzhu-btn").find("span.text").text("已关注").css({"color":"#0ae"});
                $("#guanzhu-num").css({"display":"block"});
                $("#guanzhu-num").find("em").text(watchCarIds.length);
            }
        }
        reloadGuanzhuInfoTable();
    });
    $("#guanzhu-btn").on("click",function () {
        $("#guanzhuDialog").show();
    });
    $("#guanzhuClose").on("click",function () {
        $("#guanzhuDialog").hide();
    });
    $("#carDetailsDialogClose").on("click",function () {
        $("#carDetailsDialog").hide();
    });
    $("#chaijiDialogClose").on("click",function () {
        $("#chaijiDialog").hide();
    });
    $(".box-duandian-warning").on("click",function () {
        $("#duandianReportDialog").show();
    });
    $("#duandianclose").on("click",function () {
        $("#duandianReportDialog").hide();
    });
    $(".box-zhengchang").on("click",function () {
        $("#zhengchangDialog").show();
    });
    $("#zhengchangclose").on("click",function () {
        $("#zhengchangDialog").hide();
    });

    $("#unonlineButton").on("click",function () {
        if(isSubmitData){
            return false;
        }
        var selectRows = unOnlineCarInfoTable.getAllSelections();
        var guanzhuIds = [];
        if(selectRows){
            for(var i=0;i<selectRows.length;i++){
                guanzhuIds.push(selectRows[i].id);
            }
        }
        makeZDGZfn(guanzhuIds);
        // var _watchCarIds = window.localStorage.getItem("localguanzhuData");
        // if(_watchCarIds && typeof _watchCarIds == "string"){
        //     _watchCarIds = _watchCarIds.split(",");
        //     _watchCarIds = _watchCarIds.concat(guanzhuIds);
        // }else{
        //     _watchCarIds = guanzhuIds;
        // }
        // window.localStorage.removeItem("localguanzhuData");
        // window.localStorage.setItem("localguanzhuData",_watchCarIds);
        // reloadGuanzhuInfoTable();
        //$.messageExt.alert("系统提示","重点关注成功！","success");
    });
    $("#norunlinebtn").on("click",function () {
        if(isSubmitData){
            return false;
        }
        var selectRows = norunlineInfoTable.getAllSelections();
        var guanzhuIds = [];
        if(selectRows){
            for(var i=0;i<selectRows.length;i++){
                guanzhuIds.push(selectRows[i].id);
            }
        }
        makeZDGZfn(guanzhuIds);
        // var _watchCarIds = window.localStorage.getItem("localguanzhuData");
        // if(_watchCarIds && typeof _watchCarIds == "string"){
        //     _watchCarIds = _watchCarIds.split(",");
        //     _watchCarIds = _watchCarIds.concat(guanzhuIds);
        // }else{
        //     _watchCarIds = guanzhuIds;
        // }
        // window.localStorage.removeItem("localguanzhuData");
        // window.localStorage.setItem("localguanzhuData",_watchCarIds);
        // reloadGuanzhuInfoTable();
        // $.messageExt.alert("系统提示","重点关注成功！","success");
    });
    $("#norunlinebtn").on("click",function () {
        if(isSubmitData){
            return false;
        }
        var selectRows = norunlineInfoTable.getAllSelections();
        var guanzhuIds = [];
        if(selectRows){
            for(var i=0;i<selectRows.length;i++){
                guanzhuIds.push(selectRows[i].id);
            }
        }
        makeZDGZfn(guanzhuIds);
        // var _watchCarIds = window.localStorage.getItem("localguanzhuData");
        // if(_watchCarIds && typeof _watchCarIds == "string"){
        //     _watchCarIds = _watchCarIds.split(",");
        //     _watchCarIds = _watchCarIds.concat(guanzhuIds);
        // }else{
        //     _watchCarIds = guanzhuIds;
        // }
        // window.localStorage.removeItem("localguanzhuData");
        // window.localStorage.setItem("localguanzhuData",_watchCarIds);
        // reloadGuanzhuInfoTable();
        // $.messageExt.alert("系统提示","重点关注成功！","success");
    });
    $("#unonTimelinebtn").on("click",function () {
        if(isSubmitData){
            return false;
        }
        var selectRows = unOnTimelineCarInfoTable.getAllSelections();
        var guanzhuIds = [];
        if(selectRows){
            for(var i=0;i<selectRows.length;i++){
                guanzhuIds.push(selectRows[i].id);
            }
        }
        makeZDGZfn(guanzhuIds);
        // var _watchCarIds = window.localStorage.getItem("localguanzhuData");
        // if(_watchCarIds && typeof _watchCarIds == "string"){
        //     _watchCarIds = _watchCarIds.split(",");
        //     _watchCarIds = _watchCarIds.concat(guanzhuIds);
        // }else{
        //     _watchCarIds = guanzhuIds;
        // }
        // window.localStorage.removeItem("localguanzhuData");
        // window.localStorage.setItem("localguanzhuData",_watchCarIds);
        // reloadGuanzhuInfoTable();
        // $.messageExt.alert("系统提示","重点关注成功！","success");
    });

    /**
     * 重点关注
     * @param guanzhuIds
     */
    function makeZDGZfn(guanzhuIds) {
        isSubmitData = true;
        zdgzCarIds = zdgzCarIds.concat(guanzhuIds);
        zdgzCarIds = $.ArrayUtils.unique(zdgzCarIds);
        $.post("/monitor/makeZDJKCarIds",{carIds:zdgzCarIds.toString()},function (result) {
            isSubmitData = false;
            if(result.isSuccess){
                queryGuanzhuInfoTable();
            }else{
                $.messageExt.alert("系统提示","重点关注失败！","error");
            }
        },"json")

    }
    function queryGuanzhuInfoTable(isInit) {
        $.post("/monitor/queryZDJKCarIds",null,function (result) {
            if(result.isSuccess){
                reloadGuanzhuInfoTable(result.data);
                var watchCarIds =result.data;
                $.ajax({
                    url:monitorView.queryCarCountMain_url,
                    data:{"allCarIds":watchCarIds},
                    type:"GET",
                    dataType:"JSON"
                }).done(function (result) {
                    if(result.isSuccess == true){
                        $(".totalNum").text(result.total);
                        $(".wxNum").text(result.wxnum);
                        $(".yxNum").text(result.yxnum);
                    }
                }).fail(function (error) {
                })
                //monitorView.getListMainCarCount();
                if(!isInit){
                    $.messageExt.alert("系统提示","重点关注成功！","success");
                }
                //guanzhuInfoTable.load(result.rows);
            }else{
                if(!isInit){
                    $.messageExt.alert("系统提示","重点关注失败！","error");
                }
                //guanzhuInfoTable.load([]);
            }
        },"json")
    }  
    function reloadGuanzhuInfoTable(_watchCarIds) {
        var watchCarIds = _watchCarIds;
        var baseProtocolType=$("#baseProtocolType").val();
        if(baseProtocolType=="0"){
            baseProtocolType=""
        }
        if(watchCarIds && typeof watchCarIds == "string"){
            // watchCarIds = watchCarIds.split(",");
            // if(watchCarIds.length){
            //     watchCarIds = $.ArrayUtils.unique(watchCarIds);
            // }
            var watchCarIdsArr = watchCarIds.split(",");
            if(window.localStorage.getItem("zdjkCacheData")){
                var _count = window.localStorage.getItem("zdjkCacheData");
                var _num = watchCarIdsArr.length;
                if(_count != _num){
                    $(".watchCarTotalNum").text(watchCarIdsArr.length);
                    window.localStorage.setItem("zdjkCacheData",watchCarIdsArr.length);
                }
            }else{
                $(".watchCarTotalNum").text(watchCarIdsArr.length);
                window.localStorage.setItem("zdjkCacheData",watchCarIdsArr.length);
            }
            if(zdgzCarIds.length === 0){
                zdgzCarIds = zdgzCarIds.concat(watchCarIdsArr);
            }
        }else{
            $(".watchCarTotalNum").text("0");
        }

        var params = {
            "allCarIds":watchCarIds,
            "baseProtocolType":baseProtocolType,
            "carNo":$("#carNo").val(),
            "userName":$("#userName").val(),
            "deptName":$("#deptName").val()
        };
        $.post("/monitor/listByCarIds",params,function (result) {
            if(result.isSuccess){
                guanzhuInfoTable.load(result.rows);
            }else{
                guanzhuInfoTable.load([]);
            }
        },"json")
    }

    $("#bottom-header").on("click","div.bottom-header-tab",function () {
        var $this = $(this);
        if($this.hasClass("active")){
            return false
        }
        $("#bottom-header").find("div.bottom-header-tab").removeClass("active");
        $this.addClass("active");
        $("div.bottom-table-box").css({"display":"none"});
        $("div.bottom-table").find("div[tableid='"+$this.attr("tableId")+"']").css({"display":"block"});
    })
    queryGuanzhuInfoTable(true);
    $("#searchBtn").click(function () {
        queryGuanzhuInfoTable(true);
    });
    $("#searchBaoJingBtn").click(function () {
        duandianInfoTable.refresh({
            query:{
                baseCarNo:$("#baseCarNo").val(),
                waringstate:$("#waringstate").val(),
                start:$("#start").val(),
                end:$("#end").val(),
                deptId:$("#warningDeptId").val(),
                waringType:$("#waringtype").val()
            }
        });
    });
    $("#resetSearchBaoJingBtn").click(function () {
        $("#baseCarNo").val("");
        $("#waringstate").val("")
        $("#start").val("")
        $("#end").val("")
        $("#warningDeptId").val("")
        $("#waringtype").val("")
    });

})(jQuery,window,undefined);

function _showCarInfo(index,id) {
    // $.ajax({
    //     type: "GET",
    //     url: "/baseCar/edit",
    //     cache: false,
    //     async: true,
    //     data:{
    //         id: id
    //     },
    //     dataType: "json",
    //     success: function (data) {
    //         debugger;
    //         // $('#carInfoForm').loadDate(data);
    //         for(var k in data){
    //             $("#carDetailsInfo").find("[name='"+k+"']").text(data[k] ? data[k] : "");
    //         }
    //         $("#carDetailsDialog").show();
    //         //$("#carInfoDialog").DialogExt("open");
    //     }
    // });
    $("#carDetailsDialog").show();
    $.ajax({
        type: "GET",
        url: "/monitor/editInfo",
        cache: false,
        async: true,
        data:{
            id: id
        },
        dataType: "json",
        success: function (data) {
            // $('#carInfoForm').loadDate(data);
            var _data = data.obj;
            for(var k in _data){
                $("#carDetailsInfo").find("span[name='"+k+"']").text(_data[k] ? _data[k] : "");
            }
            if(_data["baseProtocolType"] == "1"){
                $("#carDetailsInfo").find("span[name='baseProtocolType']").text("有线");
            }
            if(_data["baseProtocolType"] == "2"){
                $("#carDetailsInfo").find("span[name='baseProtocolType']").text("无线");
            }
            if(_data["baseCarTypeId"] == "1"){
                $("#carDetailsInfo").find("span[name='baseCarTypeId']").text("轿车");
            }
            if(_data["baseCarTypeId"] == "2"){
                $("#carDetailsInfo").find("span[name='baseCarTypeId']").text("面包车");
            }
            if(_data["baseCarTypeId"] == "2"){
                $("#carDetailsInfo").find("span[name='baseCarTypeId']").text("货车");
            }

            //$("#carInfoDialog").DialogExt("open");
        }
    });
};

function orderonMouseDown(e) {
    var _e = e ? e : window.event;
    _e.preventDefault();
    if(_e.which==3 ){
        var _y = _e.pageY;
        var _x = _e.pageX;
        _y = $(".monitor-box").scrollTop() + _y -72;
        $("#rightkey-box").show().css({
            "top":_y+"px",
            "left":_x+"px"
        })
    }
};

function chaijiFn() {
    $("#chaijiDialog").show();
}
